package com.lbd.bonus.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.bonus.bean.Bonus;
import com.lbd.bonus.dao.BonusRepository;
import com.lbd.bonus.service.BonusService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by Yuanbo on 2016/9/22.
 * 关于奖励类型的业务层
 */
@Service
@Transactional
public class BonusServiceImpl extends BaseServiceImpl<Bonus, BonusRepository> implements BonusService {

    @Autowired
    BonusRepository bonusRepository;

    /**
     * 奖励类型查询业务层实现
     *
     * @param bonus     要查询的实体类
     * @param parameter 筛选条件
     * @return 查询奖励类型结果
     */
    @Override
    public String buildJpql(Bonus bonus, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from  Bonus t   ");
        if (bonus != null) {
           /* if (StringUtils.isNotBlank(bonus.getTitle())) {
                jpqlBuilder.append(" and  t.title like :title");
                parameter.put("title", "%" + bonus.getTitle() + "%");
            }*/
            if (StringUtils.isNotBlank(bonus.getType())) {
                jpqlBuilder.append(" and t.type = :type");
                parameter.put("type", bonus.getType());
            }
            if (bonus.getBeginTime() != null) {
                jpqlBuilder.append(" and t.beginTime >= :beginTime");
                parameter.put("beginTime", bonus.getBeginTime());
            }
            if (bonus.getEndTime() != null) {
                jpqlBuilder.append(" and t.endTime <= :endTime");
                parameter.put("endTime", bonus.getEndTime());
            }
        }
        jpqlBuilder.append(" order by code ");
        return jpqlBuilder.toString();
    }

    /**
     * 根据bonusId 查询奖励信息
     *
     * @param id 奖励Id
     * @return 一条奖励信息
     */
    @Override
    public Bonus findById(String id) {
        return bonusRepository.findOne(id);
    }
}
